package com.txby.platformApi.component.service;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.jck.mybatis.mapper.MybatisBaseDaoCRUD;
import com.txby.alipay.AlipayConfig;
import com.txby.alipay.AlipayTradeUtil;
import com.txby.platformCore.entity.DeviceGTServerSign;
import com.txby.platformCore.entity.DeviceXGServerSign;
import com.txby.shopCore.data.AliPayConfigMode;
import com.txby.shopCore.data.WxPayConfigMode;
import com.txby.shopCore.entity.Shops;
import com.txby.wxpay.WXPayConfigImpl;
import com.txby.wxpay.WxTradeUtil;

/**
 * Spring Context 工具类
 */
@Component
public class SpringContextUtils implements ApplicationContextAware {
	public static ApplicationContext applicationContext; 
	public static DeviceXGServerSign deviceXGServerSign; 
	public static DeviceGTServerSign deviceGTServerSign; 

	private  static WxTradeUtil wxTradeUtil;
	private  static AlipayTradeUtil alipayTradeUtil;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		SpringContextUtils.applicationContext = applicationContext;
	}

	public static Object getBean(String name) {
		return applicationContext.getBean(name);
	}

	public static <T> T getBean(String name, Class<T> requiredType) {
		return applicationContext.getBean(name, requiredType);
	}

	public static boolean containsBean(String name) {
		return applicationContext.containsBean(name);
	}

	public static boolean isSingleton(String name) {
		return applicationContext.isSingleton(name);
	}

	public static Class<? extends Object> getType(String name) {
		return applicationContext.getType(name);
	}
	public static synchronized DeviceXGServerSign getDeviceXGServerSign(){
		if(deviceXGServerSign!=null){
			return deviceXGServerSign;
		}else{
			MybatisBaseDaoCRUD mBaseDao=(MybatisBaseDaoCRUD) applicationContext.getBean("mybatisBaseDaoCRUD");
			String sql="SELECT * FROM  "+mBaseDao.tableName(DeviceXGServerSign.class)+" where id='txby-xg'";
			deviceXGServerSign=(DeviceXGServerSign) mBaseDao.findBySqlUniqueObject(sql, DeviceXGServerSign.class, null);
			return deviceXGServerSign;
		}
	}
	public static synchronized DeviceGTServerSign getDeviceGTServerSign(){
		if(deviceGTServerSign!=null){
			return deviceGTServerSign;
		}else{
			MybatisBaseDaoCRUD mBaseDao=(MybatisBaseDaoCRUD) applicationContext.getBean("mybatisBaseDaoCRUD");
			String sql="SELECT * FROM  "+mBaseDao.tableName(DeviceGTServerSign.class)+" where id='txby-gt'";
			deviceGTServerSign=(DeviceGTServerSign) mBaseDao.findBySqlUniqueObject(sql, DeviceGTServerSign.class, null);
			return deviceGTServerSign;
		}
	}
	public static synchronized WxTradeUtil getWxTradeUtil(){
		if(wxTradeUtil!=null){
			return wxTradeUtil;
		}else{
			MybatisBaseDaoCRUD mBaseDao=(MybatisBaseDaoCRUD) applicationContext.getBean("mybatisBaseDaoCRUD");
			String sql="SELECT * FROM  "+mBaseDao.tableName(Shops.class)+" where id_=1";
			Shops sp= (Shops) mBaseDao.findBySqlUniqueObject(sql, Shops.class, null);
			WxPayConfigMode wm=JSON.parseObject(sp.getWechatPayInfo(), WxPayConfigMode.class);
			WXPayConfigImpl wc=WXPayConfigImpl.getInstanceNewObject(wm.getCertPath(), wm.getApp_id(), wm.getMch_id(), wm.getKey());
			  try {
				wxTradeUtil=new WxTradeUtil(wc, wm.getNotify_url());
				wxTradeUtil=new WxTradeUtil(wc, "http://jiaojing9999.free.ngrok.cc/shop/orders/WxNotifyUrl");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 
			return wxTradeUtil;
		}
	}
	public static synchronized AlipayTradeUtil getAlipayTradeUtil(){
		if(alipayTradeUtil!=null){
			return alipayTradeUtil;
		}else{
			MybatisBaseDaoCRUD mBaseDao=(MybatisBaseDaoCRUD) applicationContext.getBean("mybatisBaseDaoCRUD");
			String sql="SELECT * FROM  "+mBaseDao.tableName(Shops.class)+" where id_=1";
			Shops sp= (Shops) mBaseDao.findBySqlUniqueObject(sql, Shops.class, null);
			AliPayConfigMode am=JSON.parseObject(sp.getAliPayInfo(), AliPayConfigMode.class);
			AlipayConfig ac=new AlipayConfig();
			try {
				BeanUtils.copyProperties(ac, am);
				ac.setNotify_url("http://jiaojing9999.free.ngrok.cc/shop/orders/alipayNotifyUrl");
				alipayTradeUtil=new AlipayTradeUtil(ac);
			} catch (IllegalAccessException | InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return alipayTradeUtil;
		}
	}
	
	

}